/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel;

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.MenuItem;

import com.munchlevel.data.GameInfo;
import com.munchlevel.util.BluetoothService;

public abstract class BaseGameActivity extends BaseActivity {

	public static final String CMD_REQUEST_CONNECT_INFO = "send_connect_info";
	public static final String CMD_REQUEST_ADD_INFO = "send_add_info";

	public static final String CMD_ADD_INFO = "add_info";
	public static final String CMD_CONNECT_INFO = "connect_info";

	// Intent request codes

	public static final int REQUEST_VICTORY = 2;
	public static final int REQUEST_FIGHT = 3;
	public static final int REQUEST_CONNECT_DEVICE = 4;
	public static final int REQUEST_ENABLE_BT = 5;

	// Local Bluetooth adapter
	private BluetoothAdapter mBluetoothAdapter = null;
	// Member object for the chat services
	private BluetoothService mBluetoothService = null;

	public BaseGameActivity() {
		// Get local Bluetooth adapter
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		// If the adapter is null, then Bluetooth is not supported
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	protected boolean isBluetoothAvailable() {
		return mBluetoothAdapter != null;
	}

	protected boolean isBluetoothEnabled() {
		return (isBluetoothAvailable() && mBluetoothAdapter.isEnabled());
	}

	public GameInfo getGameInfo() {
		return MunchLevelApplication.getInstance().getGameInfo();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.discoverable:
			// Ensure this device is discoverable by others
			getBluetoothService().ensureDiscoverable();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	protected BluetoothService getBluetoothService() {

		if (mBluetoothService == null) {
			if (mBluetoothAdapter != null) {
				// Initialize the BluetoothChatService to perform bluetooth
				// connections
				mBluetoothService = new BluetoothService(this);
				initBluetoothService(mBluetoothService);
			}
		}
		return mBluetoothService;
	}

	protected abstract void initBluetoothService(BluetoothService service);

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Stop the Bluetooth chat services
		if (mBluetoothService != null)
			mBluetoothService.stop();

		// Make sure we're not doing discovery anymore
		if (mBluetoothAdapter != null) {
			mBluetoothAdapter.cancelDiscovery();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();

		// If BT is not on, request that it be enabled.
		if (isBluetoothAvailable() && isBluetoothEnabled()) {
			getBluetoothService();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Performing this check in onResume() covers the case in which BT was
		// not enabled during onStart(), so we were paused to enable it...
		// onResume() will be called when ACTION_REQUEST_ENABLE activity
		// returns.
		if (mBluetoothService != null) {
			// Only if the state is STATE_NONE, do we know that we haven't
			// started already
			if (mBluetoothService.getState() == BluetoothService.STATE_NONE) {
				// Start the Bluetooth chat services
				mBluetoothService.start();
			}
		}
	}

}
